package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import db.DBConnecter;


import entity.Transaction;
import flex.messaging.io.ArrayList;

public class TransactionDAO {
	private DBConnecter db;
	public TransactionDAO() {
		db = new DBConnecter();//initialize a new connection
	}
	public List<Transaction> queryTransactions(String userid){
		ResultSet rs;
		List<Transaction> transactiontList = new ArrayList();
		String sql = "select * from transaction where tx_from_account_id = '"+userid+"' or tx_to_account_id = '"+userid+"' ORDER BY tx_datetime DESC";
		
		try{
			rs = db.query(sql);
			while(rs.next()){
				Transaction transaction = new Transaction();
				transaction.setTx_amount(rs.getFloat("tx_amount"));
				transaction.setTx_datetime(rs.getString("tx_datetime"));
				transaction.setTx_from_account_id(rs.getString("tx_from_account_id"));
				transaction.setTx_id(rs.getString("tx_id"));
				transaction.setTx_note(rs.getString("tx_note"));
				transaction.setTx_to_account_id(rs.getString("tx_to_account_id"));
				transaction.setTx_type(rs.getString("tx_type"));
				
				
				transactiontList.add(transaction);
			}
			//System.out.println("accounttList.size"+transactiontList.size());
			rs.close();
			db.close();
			return transactiontList;
			
		}catch(SQLException e){
			e.printStackTrace();
			db.close();
			return transactiontList;
		}
		
		
		
	}
}
